package com.z6pin.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.z6pin.entity.User;
import com.z6pin.entity.Users;
import com.z6pin.mapper.UserMapper;
import com.z6pin.service.UserService;
import com.z6pin.service.impl.AsyncTaskService;

@Controller
public class LoginController {

	final private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	UserService userService;
	
	@Autowired
	AsyncTaskService asyncTaskService;
	
	@Autowired
	UserMapper userMapper;
	
	@RequestMapping("/")
	public String index(Map<String, Object> model) {
		return "index";
	}
	
	@RequestMapping("/index.html")
	public String toindex(Map<String, Object> model) {
		return "index";
	}
	
	@RequestMapping("/login.html")
	public String loginhtml(Map<String, Object> model) {
		return "login";
	}
	
	@RequestMapping("/header.html")
	public String header(Map<String, Object> model) {
		return "header";
	}
	
	@RequestMapping("/footer.html")
	public String footer(Map<String, Object> model) {
		return "footer";
	}
	
	@RequestMapping("/register.html")
	public String registerhtml(Map<String, Object> model) {
		return "register";
	}
	
	@RequestMapping("/register")
	public String register(User user,Map<String, Object> model) {
		System.out.println("user.name="+user.getName());
		userMapper.insert(user);
		model.put("message", "注册成功，请登录");
		
		return "login";
	}
	
	@RequestMapping("/login")
	public String login(User user,Map<String, Object> model) {
		
		User userLogin = userMapper.checkPassword(user);
		if(userLogin == null) {
			model.put("message", "账号验证失败");
			return "login";
		} else {
			model.put("message", "登录成功");
		}
		
		List<Users> userList = userService.getAll();
        model.put("time", new Date());
        model.put("users", userList);
        
		return "demo";
	}
	
	@RequestMapping("/findpassword.html")
	public String findpassword(Map<String, Object> model) {
		return "findpassword";
	}
	
	@RequestMapping("/editResume.html")
	public String editResume(Map<String, Object> model) {
		return "editResume";
	}
	
	@RequestMapping("/introduction.html")
	public String introduction(Map<String, Object> model) {
		return "introduction";
	}
	
	@RequestMapping("/demo")
	public String excudeService(Map<String, Object> model) {
		try {
			List<Users> userList = userService.getAll();
	        model.put("time", new Date());
	        model.put("message", "Thymeleaf模板案列");
	        model.put("users", userList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "demo";
	}
	
	@RequestMapping("/reduce")
	public String descAccount(Map<String, Object> model) {
		try {
			for(int i=0;i<500;i++) {
				asyncTaskService.executeAysncTaskReduce(i);
			}
		}catch(Exception e) {
			System.out.println(e.getMessage());
		}
		
		List<Users> userList = userService.getAll();
        model.put("time", new Date());
        model.put("message", "jsp测试");
        model.put("users", userList);
        
		return "demo";
	}

    @RequestMapping(value="/webgl")
    public String testWebGL(){
        return "webgl";
    }

}